﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManageSystem
{
    public partial class BorrowBook : Form
    {
        public BorrowBook()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            if (textBox6.Text != "")
            {
                string str = "select bookName,bookAuthor from bookInfo where bookId=" + textBox6.Text;
                SqlDataReader sqlDr = Utils.getDataReader(str);
                try
                {
                    if (sqlDr.Read())
                    {
                        textBox5.Text = sqlDr[0].ToString();
                        textBox4.Text = sqlDr[1].ToString();
                    }
                    else
                    {
                        textBox5.Text = "";
                        textBox4.Text = "";
                    }
                }
                catch
                {
                    textBox5.Text = "";
                    textBox4.Text = "";
                }
            }
            else
            {
                textBox5.Text = "";
                textBox4.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox5.Text == "")
            {
                MessageBox.Show("请输入图书编号！");
                return;
            }
            else if (textBox9.Text == "")
            {
                MessageBox.Show("请输入读者编号！");
                return;
            }
            string str = "delete from borrowInfo where bookId=" + textBox6.Text + " and userId=" + textBox1.Text;
            if (Utils.ExecuteNoQuery(str) != -1)
            {
                MessageBox.Show("还书成功");

                ClearData();
            }
            else
            {
                MessageBox.Show("还书失败");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ClearData();

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                string str = "select userName from UserInfo where userId=" + textBox1.Text;
                SqlDataReader sqlDr = Utils.getDataReader(str);
                if (sqlDr.Read())
                {
                    textBox9.Text = sqlDr[0].ToString();
                }
                else
                {
                    textBox9.Text = "";

                }
            }
            else
                textBox9.Text = "";

        }


        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox5.Text == "")
            {
                MessageBox.Show("请输入图书编号！");
                return;
            }
            else if (textBox9.Text == "")
            {
                MessageBox.Show("请输入读者编号！");
                return;
            }
            string str = "insert borrowInfo(bookId,bookName,bookAuthor,userId,userName,borrowTime) values(" + textBox6.Text + ",N'" + textBox5.Text + "',N'" + textBox4.Text + "',N'" + textBox1.Text + "',N'" + textBox9.Text + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "')";
            if (Utils.ExecuteNoQuery(str) != -1)
            {
                MessageBox.Show("借阅成功");

                ClearData();
            }
            else
            {
                MessageBox.Show("借阅失败");
            }
        }

        private void ClearData()
        {
            textBox1.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox9.Text = "";

        }
    }
}
